#include "PointEmitter.h"

CPointEmitter::CPointEmitter(void): m_position(this, Vector3(0, 0, 0)){}

CPointEmitter::~CPointEmitter(void){}

void CPointEmitter::emitterUpdate(void)
{
	//get the array of particles and the number of particle
	float* arrayOfParticleSystem=m_particleSystem->m_arrayOfParticles;
	int numberOfParticle=m_particleSystem->m_numberOfParticle;

	//for every particle to emit
	for(int i=0; i<m_numberOfParticleToEmit.getValue(); i++)
	{
		int position=0;
		const int numberOfElementPerParticle=8;

		//look for the index of an empty particle
		while(arrayOfParticleSystem[position+7]!=-1 && position<numberOfParticle*numberOfElementPerParticle)
		{
			position+=numberOfElementPerParticle;
		}

		//if we did'nt find an empty particle = exit
		if(position>=numberOfParticle*numberOfElementPerParticle)
			return;

		//set the position of the particle
		arrayOfParticleSystem[position]=m_position.getValue().x;
		arrayOfParticleSystem[position+1]=m_position.getValue().y;
		arrayOfParticleSystem[position+2]=m_position.getValue().z;

		//set the speed
		arrayOfParticleSystem[position+3]=(-5+rand()%10)*m_velocity.getValue().x;
		arrayOfParticleSystem[position+4]=(-5+rand()%10)*m_velocity.getValue().y;
		arrayOfParticleSystem[position+5]=(-5+rand()%10)*m_velocity.getValue().z;

		//set time and alive
		arrayOfParticleSystem[position+6]=m_particleSystem->m_time.getValue();
		arrayOfParticleSystem[position+7]=1;
	}
}
